// package-activity/pages/activity-edit-product/index.js
const app = getApp();
import { PACKAGE_STAR_PAGE, PACKAGE_PRODUCT_PAGE } from '../../../constants/route';
import { TARGET_TYPE } from '../../../constants/common';
import { ACTIVITY_PRODUCT_MAX } from '../../../constants/activity';
import { ManageTargetInfoUpdateMixins } from '../../../behaviors/page';
import { navTo, navBack, tipToast } from '../../../common/env';
import { arr2Obj } from '../../../common/utils';

Page({
    mixins: [ManageTargetInfoUpdateMixins],
    data: {
        TARGET_TYPE,
        activityInfo: {},
        checkedList: [],
    },
    onLoad(options) {
        app.event.addListener('sendActivityInfo', this._handleSendActivity.bind(this));
    },
    onUnload() {
        app.event.removeListener('sendActivityInfo');
    },
    _handleSendActivity(activityInfo) {
        const checkedList = activityInfo.activityProductWithSkus.map(i => i.productId);
        this.setData({
            activityInfo,
            checkedList,
        });
    },
    handleNavToCreate() {
        navTo(PACKAGE_PRODUCT_PAGE['productEdit']);
    },
    handleProductChecked(e) {
        const { ids, products } = e.detail;
        const idsMap = arr2Obj(ids);
        let { activityProductWithSkus = [], consignment } = this.data.activityInfo;
        const consignmentBonusSettingsMap = arr2Obj(consignment.consignmentBonusSettings, 'productId');
        products.forEach(p => {
            const skus = p.productSpecs && p.productSpecs.length ? p.productSpecs.map(s => {
                s.productId = p.productId;
                s.maximum = '';
                s.skuName = s.name;
                return s;
            }) : [{
                productId: p.productId,
                productSpecId: '0',
                price: p.priceMin,
                maximum: '',
            }];
            const ps = {
                productId: p.productId,
                productName: p.name,
                productPics: p.pics,
                priceMin: p.priceMin,
                priceMax: p.priceMax,
                skus: skus,
                isShow: true,
            };
            if (activityProductWithSkus.findIndex(i => i.productId === p.productId) === -1) {
                activityProductWithSkus.push(ps);
            }
        });
        activityProductWithSkus = activityProductWithSkus.filter(p => idsMap[p.productId]);
        consignment.consignmentBonusSettings = activityProductWithSkus.map(ps => ({
            productId: ps.productId,
            bonusRate: consignmentBonusSettingsMap[ps.productId] ? consignmentBonusSettingsMap[ps.productId].bonusRate : '',
        }));
        this.setData({
            checkedList: ids,
            'activityInfo.activityProductWithSkus': activityProductWithSkus,
            'activityInfo.consignment': consignment,
        });
    },
    handleSave() {
        const { activityInfo } = this.data;
        if (!activityInfo.activityProductWithSkus || !activityInfo.activityProductWithSkus.length) {
            tipToast('请选择商品');
            return;
        } else if (activityInfo.activityProductWithSkus.length > ACTIVITY_PRODUCT_MAX) {
            tipToast(`最多添加${ACTIVITY_PRODUCT_MAX}个商品`);
            return;
        }
        app.event.trigger('getActivityInfo', activityInfo);
        navBack();
    },
})